﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RecognitionForm
{
    public static class JsonExt
    {
        public static string SerializeObject(this object obj)
        {
            //IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            //timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            //return JsonConvert.SerializeObject(obj, Formatting.Indented, timeFormat);

            return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });
        }

        public static T DeserializeObject<T>(this string data)
        {
            //IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            //timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            //return JsonConvert.DeserializeObject<T>(data, timeFormat);

            return JsonConvert.DeserializeObject<T>(data, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });

        }

        public static float[,] ConvertFloat(this int[,] a)
        {
            var x = a.GetLength(0);
            var y = a.GetLength(1);
            float[,] result = new float[x, y];
            for (var i = 0; i < x; i++)
                for (var j = 0; j < y; j++)
                    result[i, j] = a[i, j];

            return result;
        }
    }
}
